package com.example.administrator.newproject;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

import Bean.ShouCang;
import Utils.DbUtil;

public class ShouCangActivity extends ActionBarActivity {

    private List<ShouCang> shouCangs;
    private List<ShouCang> cangs;
    private DbUtils dbUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shou_cang);
        ListView saveList = (ListView) findViewById(R.id.shoucanglist);

        dbUtils = DbUtil.getDbUtils(ShouCangActivity.this);


      /*  ShouCang cang =new ShouCang("imagePath","url","title22");
        try {
          dbUtils.save(cang);
            //dbUtils.deleteAll(ShouCang.class);

        } catch (DbException e) {
            e.printStackTrace();
        }*/

      try {
            List<ShouCang> cangs = this.dbUtils.findAll(Selector.from(ShouCang.class)
                    // .where("id", "=", size)
                    .orderBy("_id", true)
                    .limit(10)
                    .offset(0));
            if(cangs.size()<=10){
                setData(saveList, cangs);

            }else{
             // Toast.makeText(ShouCangActivity.this,"===101010=",Toast.LENGTH_LONG).show();
                dbUtils.delete(Selector.from(ShouCang.class).where("_id","=",1));
                dbUtils.update(Selector.from(ShouCang.class));
                dbUtils.save(cangs.get(11));
                dbUtils.update(Selector.from(ShouCang.class));
                        setData(saveList,cangs);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

        saveList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShouCang shouCang = cangs.get(position);
                String url = shouCang.getUrl();
                Intent i = new Intent();






            }
        });
    }
    private void setData(ListView saveList, List<ShouCang> cangs) {
        ShouCangListAdapter scAdapter = new ShouCangListAdapter(ShouCangActivity.this, cangs);

        saveList.setAdapter(scAdapter);
    }

}
